<?php

namespace Kunfeng\Dify\Model\Chat;

use Kunfeng\Dify\Enum\ResponseMode;

/**
 * 对话消息请求模型
 */
class ChatMessage
{
    private $query;
    private $user;
    private $responseMode;
    private $inputs;
    private $conversationId;
    private $files;
    private $autoGenerateName;

    public function __construct(
        string $query,
        string $user,
        string $responseMode = ResponseMode::BLOCKING,
        ?array $inputs = null,
        ?string $conversationId = null,
        ?array $files = null,
        bool $autoGenerateName = true
    ) {
        $this->query = $query;
        $this->user = $user;
        $this->responseMode = $responseMode;
        $this->inputs = $inputs;
        $this->conversationId = $conversationId;
        $this->files = $files;
        $this->autoGenerateName = $autoGenerateName;
    }

    /**
     * 创建消息
     */
    public static function create(string $query, string $user): self
    {
        return new self($query, $user);
    }

    /**
     * 使用建造者模式
     */
    public static function builder(): ChatMessageBuilder
    {
        return new ChatMessageBuilder();
    }

    /**
     * 添加输入参数
     */
    public function withInputs(array $inputs): self
    {
        return new self(
            $this->query,
            $this->user,
            $this->responseMode,
            $inputs,
            $this->conversationId,
            $this->files,
            $this->autoGenerateName
        );
    }

    /**
     * 设置响应模式
     */
    public function withResponseMode(string $responseMode): self
    {
        return new self(
            $this->query,
            $this->user,
            $responseMode,
            $this->inputs,
            $this->conversationId,
            $this->files,
            $this->autoGenerateName
        );
    }

    /**
     * 设置会话ID
     */
    public function withConversationId(string $conversationId): self
    {
        return new self(
            $this->query,
            $this->user,
            $this->responseMode,
            $this->inputs,
            $conversationId,
            $this->files,
            $this->autoGenerateName
        );
    }

    /**
     * 转换为数组
     */
    public function toArray(): array
    {
        $data = [
            'query' => $this->query,
            'user' => $this->user,
            'response_mode' => $this->responseMode,
            'auto_generate_name' => $this->autoGenerateName,
        ];

        if ($this->inputs !== null) {
            $data['inputs'] = $this->inputs;
        }
        if ($this->conversationId !== null) {
            $data['conversation_id'] = $this->conversationId;
        }
        if ($this->files !== null) {
            $data['files'] = $this->files;
        }

        return $data;
    }

    // Getters
    public function getQuery(): string
    {
        return $this->query;
    }

    public function getUser(): string
    {
        return $this->user;
    }

    public function getResponseMode(): string
    {
        return $this->responseMode;
    }

    public function getInputs(): ?array
    {
        return $this->inputs;
    }

    public function getConversationId(): ?string
    {
        return $this->conversationId;
    }

    public function getFiles(): ?array
    {
        return $this->files;
    }

    public function getAutoGenerateName(): bool
    {
        return $this->autoGenerateName;
    }
}

/**
 * 对话消息构建器
 */
class ChatMessageBuilder
{
    private $query;
    private $user;
    private $responseMode = ResponseMode::BLOCKING;
    private $inputs;
    private $conversationId;
    private $files;
    private $autoGenerateName = true;

    public function query(string $query): self
    {
        $this->query = $query;
        return $this;
    }

    public function user(string $user): self
    {
        $this->user = $user;
        return $this;
    }

    public function responseMode(string $responseMode): self
    {
        $this->responseMode = $responseMode;
        return $this;
    }

    public function inputs(array $inputs): self
    {
        $this->inputs = $inputs;
        return $this;
    }

    public function conversationId(string $conversationId): self
    {
        $this->conversationId = $conversationId;
        return $this;
    }

    public function files(array $files): self
    {
        $this->files = $files;
        return $this;
    }

    public function autoGenerateName(bool $autoGenerateName): self
    {
        $this->autoGenerateName = $autoGenerateName;
        return $this;
    }

    public function build(): ChatMessage
    {
        if (!isset($this->query)) {
            throw new \InvalidArgumentException('query is required');
        }
        if (!isset($this->user)) {
            throw new \InvalidArgumentException('user is required');
        }

        return new ChatMessage(
            $this->query,
            $this->user,
            $this->responseMode,
            $this->inputs,
            $this->conversationId,
            $this->files,
            $this->autoGenerateName
        );
    }
}
